unit UnitFormPreinformes;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
  DBGrids, sqldb, db, unitmainform, unitnuevopreinforme;

type

  { TFormPreinformes }

  TFormPreinformes = class(TForm)
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    MenuItem9: TMenuItem;
    SQLQuery1: TSQLQuery;
    procedure DBGrid1CellClick(Column: TColumn);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow();
    procedure MenuItem2Click(Sender: TObject);
    procedure MenuItem4Click(Sender: TObject);
    procedure MenuItem5Click(Sender: TObject);
    procedure MenuItem6Click(Sender: TObject);
    procedure MenuItem8Click(Sender: TObject);
    procedure MenuItem9Click(Sender: TObject);
    procedure ordenar_grid();
  private
    { private declarations }
  public
    titulo : string;
  end; 

var
  FormPreinformes: TFormPreinformes;

implementation

uses
  unitmodificarpreinforme;

{$R *.lfm}

{ TFormPreinformes }

procedure TFormPreinformes.ordenar_grid();
begin
  if SQLQuery1.Active = true then
    SQLQuery1.Close;

  SQLQuery1.SQL.Text := 'select * from preinforme where prinf_titulo like ''%' + titulo
  + '%'' collate nocase order by prinf_titulo';

  SQLQuery1.Open;

  DBGrid1.Columns[0].Title.Caption := 'Código';
  DBGrid1.Columns[1].Title.Caption := 'Título';

  DBGrid1.Columns[0].Width := 70;
  DBGrid1.Columns[1].Width := 540;

  DBGrid1.Columns[2].Visible := false;

end;

procedure TFormPreinformes.MenuItem2Click(Sender: TObject);
begin
  Close();
end;

procedure TFormPreinformes.MenuItem4Click(Sender: TObject);
begin
  formnuevopreinforme.ShowModal;

  SQLQuery1.Close;
  SQLQuery1.SQL.Text := 'select * from preinforme';
  SQLQuery1.Open;

  SQLQuery1.ApplyUpdates;
  MainForm.SQLTransaction1.Commit;
  MainForm.ordenar_grid();

  ordenar_grid();
end;

procedure TFormPreinformes.MenuItem5Click(Sender: TObject);
begin
  ShowMessage('Click en el preinforme a modificar');
end;

procedure TFormPreinformes.MenuItem6Click(Sender: TObject);
var
  codigo : string;
begin
  codigo := InputBox('Eliminar Preinforme', 'Ingrese el código de preinforme a eliminar', '');

  if codigo <> '' then
  begin
    if SQLQuery1.Active = true then
      SQLQuery1.Close;

    SQLQuery1.SQL.Text := 'delete from preinforme where prinf_cdg = ' + codigo;

    SQLQuery1.ExecSQL;

    SQLQuery1.SQL.Text := 'select * from preinforme';

    SQLQuery1.Open;

    SQLQuery1.ApplyUpdates;
    MainForm.SQLTransaction1.Commit;
    MainForm.ordenar_grid();

    ShowMessage('Baja Exitosa');

    ordenar_grid();
  end;
end;

procedure TFormPreinformes.MenuItem8Click(Sender: TObject);
begin
  titulo := InputBox('Filtro por Título', 'Ingrese parte del título:', '');

  if titulo <> '' then
    ordenar_grid()
  else
    FormShow();
end;

procedure TFormPreinformes.MenuItem9Click(Sender: TObject);
begin
  FormShow();
end;

procedure TFormPreinformes.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = true then
    SQLQuery1.Close;
end;

procedure TFormPreinformes.DBGrid1CellClick(Column: TColumn);
begin
  FormModificarPreinforme.Label3.Caption := SQLQuery1.FieldByName('prinf_cdg').AsString;
  FormModificarPreinforme.Edit1.Text := SQLQuery1.FieldByName('prinf_titulo').AsString;
  FormModificarPreinforme.Memo1.Text := SQLQuery1.FieldByName('prinf_texto').AsString;

  FormModificarPreinforme.ShowModal;

  SQLQuery1.Close;
  SQLQuery1.SQL.Text := 'select * from preinforme';
  SQLQuery1.Open;
  SQLQuery1.ApplyUpdates;
  MainForm.SQLTransaction1.Commit;
  MainForm.ordenar_grid();

  ordenar_grid();
end;

procedure TFormPreinformes.FormCreate(Sender: TObject);
begin
  SQLQuery1.Database := MainForm.SQLite3Connection1;
  SQLQuery1.Transaction := MainForm.SQLTransaction1;
end;

procedure TFormPreinformes.FormShow();
begin
  titulo := '%';
  ordenar_grid();
end;

end.

